﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace Entity
{
    public class ApiDBContext : DbContext
    {

        public ApiDBContext(DbContextOptions<ApiDBContext> opts) : base(opts)
        {
        }
        /// <summary>
        /// 用户信息数据模型
        /// </summary>
        public DbSet<EntityUser> EntityUser { get; set; }

        //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        //{
        //    optionsBuilder.UseMySql("server=localhost;userid=root;password=123456;database=AIYDB;port=3306l;", new MySqlServerVersion(new Version(9, 2, 0)));
        //}
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            EntityUser user = new EntityUser();
            user.FirstName = "123123";
            user.LastName = "3456";
            modelBuilder.Entity<EntityUser>().HasData([
                new EntityUser(){
                    ID=Guid.NewGuid(),
                    Age=16,
                    Email="admin@admin.com",
                    FirstName="张",
                    LastName="三",
                    PhoneNumber="19546281632",
                },
                new EntityUser(){
                    ID=Guid.NewGuid(),
                    Age=18,
                    Email="admin@list.com",
                    FirstName="李",
                    LastName="四",
                    PhoneNumber="19546281632",
                }
            ]);
        }
    }
    /// <summary>
    /// 配置EF创建方式，以及数据库配置
    /// </summary>
    public class ApiDbContextFactory : IDesignTimeDbContextFactory<ApiDBContext>
    {
        public ApiDBContext CreateDbContext(string[] args)  
        {
            var optionBulider = new DbContextOptionsBuilder<ApiDBContext>();
            optionBulider.UseMySql(
            "server=localhost;userid=root;password=123456;database=AIYDB;port=3306l;",
            new MySqlServerVersion(new Version(8, 0, 41)));
            return new ApiDBContext(optionBulider.Options);
        }
    }
}